import pygame
import sys #退出程序时用到

pygame.init()#初始化pygame

size = width,height = 600,400
speed = [2,1]
bg = (255,255,255)
screen = pygame.display.set_mode(size)#窗口大小
pygame.display.set_caption('Hello!')#窗口标题
thing = pygame.image.load('yd.png')#加载图片
position = thing.get_rect()#获得图像位置矩形

while True:
    for event in pygame.event.get():#事件队列循环
        if event.type == pygame.QUIT:
             sys.exit()#系统退出

    position = position.move(speed)#移动图像

    if position.left < 0 or position.right > width:#检测边界，左上角(0,0)
        thing = pygame.transform.flip(thing,True,False)#翻转图像
        speed[0] = -speed[0]#反向运动

    if position.top < 0 or position.bottom > height:
        speed[1] = -speed[1]#反向运动

    screen.fill(bg)#填充背景
    screen.blit(thing,position)#更新图像,将thing画到screen上
    pygame.display.flip()#更新界面
    pygame.time.delay(10)#延时
